package com.yunxi.service.ups.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.claim.pojo.*;
import com.yunxi.claim.service.*;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.pojo.ups.FailureCauseVO;
import com.yunxi.eai.pojo.ups.FaultTreeAnalysisVO;
import com.yunxi.eai.pojo.ups.MaintainOrderVO;
import com.yunxi.eai.service.ups.UpsService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.parts.pojo.PrPartPriceUpsReqVO;
import com.yunxi.parts.service.EaiPartDubboService;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/9/1.
 */
@Service
@Qualifier("upsServiceImpl")
public class UpsServiceImpl implements UpsService {
    private static final Logger logger = LoggerFactory.getLogger(UpsServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Autowired
    private CmTroublePartDubboService cmTroublePartDubboServiceImpl;
    @Autowired
    private CmTroubleInfoDubboService cmTroubleInfoDubboServiceImpl;
    @Autowired
    private EaiPartDubboService eaiPartDubboServiceImpl;
    @Autowired
    private Mapper dozerMapper;
    @Autowired
    EaiApiCmWrSensitiveDubboService eaiApiCmWrSensitiveDubboServiceImpl;
    @Autowired
    EaiApprovePartDubboService cmWrApprovePartDubboServiceImpl;
    @Override
    public ApiResult distributeUps(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.UPS_PMS_FAULT_TREE_ANALYSIS)) {
            //故障原因分类树
            apiResult = saveFaultTreeAnalysis(esbVO);
        }else if (serviceName.equals(Constant.UPS_PMS_FAULT_CAUSE)){
            //故障原因
            apiResult = saveFailureCause(esbVO);
        }else if (serviceName.equals(Constant.UPS_PMS_PART_PRICE)){
            //渠道商配件库存价格信息
            apiResult = findPartPrice(esbVO);
        }else if (serviceName.equals(Constant.UPS_PMS_THREE_PACK_WARN)){
            //三包预警
            apiResult = findThreePackWarn(esbVO);
        }else if (serviceName.equals(Constant.UPS_PMS_CLAIM_CKECKOUT)){
            //索赔合理性校验
            apiResult = findCaimCkeckOut(esbVO);
        }else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }



    private ApiResult findCaimCkeckOut(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS工单系统进行索赔合理性校验");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            CmApproveCheckReqVO cmApproveCheckReqVO=JSONObject.toJavaObject(JSON.parseObject(reqBody),CmApproveCheckReqVO.class);
            apiResult= cmWrApprovePartDubboServiceImpl.approveCheck(cmApproveCheckReqVO);
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult findThreePackWarn(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS工单系统获取三包预警信息");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            JSONObject jsonObject = JSONObject.parseObject(reqBody);
            apiResult= eaiApiCmWrSensitiveDubboServiceImpl.findByVin(String.valueOf(jsonObject.get("vin")));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            apiResult=ResponseUtil.fail();
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult findPartPrice(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS工单系统获取渠道商配件库存价格信息");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            ArrayList<PrPartPriceUpsReqVO> var1 = JSON.parseObject(reqBody, new TypeReference<ArrayList<PrPartPriceUpsReqVO>>() {});
            apiResult = eaiPartDubboServiceImpl.findPartPrices4Ups(var1);
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveFailureCause(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        //  FailureCauseVO failureCauseVO = JSONObject.toJavaObject(JSON.parseObject(reqBody), FailureCauseVO.class);
        ArrayList<FailureCauseVO> failureCauList = JSON.parseObject(reqBody, new TypeReference<ArrayList<FailureCauseVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS工单系统传递故障原因给UPS保修系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmTroubleInfoReqVO> var1=new ArrayList<>();
            for (int i = 0; i < failureCauList.size(); i++) {
                CmTroubleInfoReqVO cmTroubleInfoReq = dozerMapper.map(failureCauList.get(i), CmTroubleInfoReqVO.class);
                var1.add(cmTroubleInfoReq);
            }
            cmTroubleInfoDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            apiResult=ResponseUtil.ok();
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveFaultTreeAnalysis(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        //FaultTreeAnalysisVO faultTreeAnalysisVO = JSONObject.toJavaObject(JSON.parseObject(reqBody), FaultTreeAnalysisVO.class);
        ArrayList<FaultTreeAnalysisVO> faultTreeAnalysisList = JSON.parseObject(reqBody, new TypeReference<ArrayList<FaultTreeAnalysisVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS工单系统传递故障原因分类树给UPS保修系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmTroublePartReqVO> var1=new ArrayList<>();
            for (int i = 0; i < faultTreeAnalysisList.size(); i++) {
                CmTroublePartReqVO cmTroublePartReq = dozerMapper.map(faultTreeAnalysisList.get(i), CmTroublePartReqVO.class);
                var1.add(cmTroublePartReq);
            }
            cmTroublePartDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            apiResult=ResponseUtil.ok();
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }
}
